// const url = 'http://localhost:8080/api';
const url = 'http://hourapi.jsrpx.cn/api';
const $get = (...args) => fetch(...args).then(res => res.json(), () => Promise.resolve({success: false, msg: '网络错误'}));
new Vue({
    el: '#app',
    data: {
        query: '130603199902020927',
        trainTime: [],
        user: {},
        push: [],
        study: [],
        catalog: [],
        history: [],
        HOUR_TYPE: {
            1: '理论',
            2: '实操',
            3: '模拟'
        }
    },
    methods: {
        search () {
            const vm = this;
            if (!vm.query) {
                alert('请输入证件号码');
                return;
            }
            vm.reset();
            vm.history.unshift({
                cardNum: vm.query,
                date: new Date().getTime()
            });
            if (vm.history.length > 20) {
                vm.history.length = 20;
            }
            vm.$trainTimes(vm.query);
            vm.$user(vm.query);
        },
        reQuery (cardNum) {
            this.query = cardNum;
            this.search();
        },
        expand (row) {
            const vm = this;
            row.$expand = !row.$expand;
            if (row.hasExpanded) {
                return;
            }
            row.hasExpanded = true;
            vm.getData(row);
        },
        reset () {
            this.push = [];
            this.study = [];
            this.catalog = [];
        },
        getData (trainTime) {
            const vm = this;
            vm.$push(trainTime.cardNum, trainTime.trainCarType);
            vm.$time(trainTime.cardNum, trainTime.trainCarType, vm.user.source);
            vm.$catalogs(trainTime.trainCarType);
        },
        transform (v, isDateTime) {
            if (isDateTime) {
                return new Date(v).toLocaleString();
            }
            return new Date(v).toLocaleDateString();
        },
        $trainTimes (cardNum) {
            const vm = this;
            $get(`${url}/time/gather?cardNum=${cardNum}`).then(r => {
                if (!r.success) {
                    alert(r.msg || '网络错误');
                    return;
                }
                vm.trainTime = r.data.map(d => Object.assign({$expand: false}, d));
            });
        },
        $user (cardNum) {
            const vm = this;
            $get(`${url}/user?cardNum=${cardNum}`).then(r => {
                if (!r.success) {
                    alert(r.msg || '网络错误');
                    return;
                }
                vm.user = r.data[0] || {};
            });
        },
        $push (cardNum, cardType) {
            const vm = this;
            $get(`${url}/logs/push?cardNum=${cardNum}&carType=${cardType}`).then(r => {
                if (!r.success) {
                    alert(r.msg || '网络错误');
                    return;
                }
                vm.push = r.data;
            });
        },
        $time (cardNum, cardType, source) {
            const vm = this;
            $get(`${url}/logs/time?cardNum=${cardNum}&carType=${cardType}&source=${source}`).then(r => {
                if (!r.success) {
                    alert(r.msg || '网络错误');
                    return;
                }
                vm.study = r.data;
            });
        },
        $catalogs (cardType) {
            const vm = this;
            $get(`${url}/catalogs/${cardType}`).then(r => {
                if (!r.success) {
                    alert(r.msg || '网络错误');
                    return;
                }
                vm.catalog = r.data;
            });
        },
        $catalog (cardType, version) {
            const vm = this;
            $get(`${url}/catalogs/${cardType}/${version}`).then(r => {
                if (!r.success) {
                    alert(r.msg || '网络错误');
                    return;
                }
                vm.trainTime = r.data;
            });
        },
        $repush (id) {
            return $get(`${url}/repush/${id}`);
        },
        $maxRepush (id) {
            return $get(`${url}/repush/max/${id}`);
        }
    }
});